package com.gavindon.android.viewmodel

import androidx.lifecycle.viewModelScope
import com.gavindon.android.base.BaseViewModel
import com.gavindon.android.entity.*
import com.gavindon.android.network.DataResult
import com.gavindon.android.repo.ComboRepo
import kotlinx.coroutines.launch

/**
 * description:
 * Created by liNan on  2022-2-7 09:46
 */
class ComboViewModel : BaseViewModel() {

    private val repo = ComboRepo()


    private val orderInfoLiveData = SingleLiveEvent<DataResult<OrderInfoResp>>()
    private val aliOrderInfoLiveData = SingleLiveEvent<DataResult<OrderInfoResp>>()
    private val wxQrLiveData = SingleLiveEvent<DataResult<String>>()
    private val aliQrLiveData = SingleLiveEvent<DataResult<String>>()
    private val checkPayLiveData = SingleLiveEvent<DataResult<PayResultResp>>()
    private val comboInfoLiveData = SingleLiveEvent<DataResult<ComboResp>>()
    private val payLookLiveData = SingleLiveEvent<DataResult<List<CheckCanLookResp>>>()
    private val payLookLiveData2 = SingleLiveEvent<DataResult<List<CheckCanLookResp>>>()
    private val myCourseOrPackageLiveData = SingleLiveEvent<DataResult<List<CheckCanLookResp>>>()
    private val myCourseOrPackage2LiveData = SingleLiveEvent<DataResult<List<CheckCanLookResp>>>()
    fun getCombLstById(id: String): SingleLiveEvent<DataResult<ComboResp>> {
        viewModelScope.launch {
            val resp = repo.getComboInfo(id)
            comboInfoLiveData.value = resp
        }
        return comboInfoLiveData
    }


    fun getOrderId(
        itemType: String,
        courseId: String,
        payType: String
    ): SingleLiveEvent<DataResult<OrderInfoResp>> {
        viewModelScope.launch {
            val resp = repo.generateOrderId(itemType, courseId, payType)
            orderInfoLiveData.value = resp
        }
        return orderInfoLiveData
    }

    fun getAliOrderId(
        itemType: String,
        courseId: String,
        payType: String
    ): SingleLiveEvent<DataResult<OrderInfoResp>> {
        viewModelScope.launch {
            val resp = repo.generateOrderId(itemType, courseId, payType)
            aliOrderInfoLiveData.value = resp
        }
        return aliOrderInfoLiveData
    }

    fun getWxPayQr(orderId: String): SingleLiveEvent<DataResult<String>> {
        viewModelScope.launch {
            val resp = repo.wxPayQr(orderId)
            wxQrLiveData.value = resp
        }
        return wxQrLiveData
    }

    fun getAliPayQr(orderId: String): SingleLiveEvent<DataResult<String>> {
        viewModelScope.launch {
            val resp = repo.aliPayQr(orderId)
            aliQrLiveData.value = resp
        }
        return aliQrLiveData
    }

    /**
     * 查询是否支付
     */
    fun checkIsPay(orderId: String): SingleLiveEvent<DataResult<PayResultResp>> {
        viewModelScope.launch {
            val resp = repo.checkIsPay(orderId)
            checkPayLiveData.value = resp
        }
        return checkPayLiveData
    }

    /**
     * 查询是否可以观看
     */
    fun checkIsCanLook(
        orderType: String,
        packageOrCourseId: String
    ): SingleLiveEvent<DataResult<List<CheckCanLookResp>>> {
        viewModelScope.launch {
            val resp = repo.payFinish(orderType, packageOrCourseId)
            payLookLiveData.postValue(resp)
        }
        return payLookLiveData
    }

    //使用不同的livedata
    fun checkIsCanLook2(
        orderType: String,
        packageOrCourseId: String
    ): SingleLiveEvent<DataResult<List<CheckCanLookResp>>> {
        viewModelScope.launch {
            val resp = repo.payFinish(orderType, packageOrCourseId)
            payLookLiveData2.postValue(resp)
        }
        return payLookLiveData2
    }

    //套餐
    fun myCourseOrPackage(
        orderType: String,
        packageOrCourseId: String
    ): SingleLiveEvent<DataResult<List<CheckCanLookResp>>> {
        viewModelScope.launch {
            val resp = repo.myCourseOrPackage(1)
            myCourseOrPackageLiveData.postValue(resp)
        }
        return myCourseOrPackageLiveData
    }

    //课程
    fun myCourseOrPackage2(
        orderType: String,
        packageOrCourseId: String
    ): SingleLiveEvent<DataResult<List<CheckCanLookResp>>> {
        viewModelScope.launch {
            val resp = repo.myCourseOrPackage(0)
            myCourseOrPackage2LiveData.postValue(resp)
        }
        return myCourseOrPackage2LiveData
    }
}